package com.ews.onlineexchange.service;

import java.util.List;

import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.approved.ApprovedMatter;

@Named
@ViewScoped
public class ApprovedMatterList extends EntityQuery<ApprovedMatter> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	ApprovedClassHome approvedClassHome;
	
	@Inject
	ViolationRecordHome violationRecordHome;

	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<ApprovedMatter> entity) {		
		List<Predicate> restrictions = super.getRestrictions(cb, entity);
		
		if (approvedClassHome.isManaged())
			restrictions.add(cb.equal(entity.get("approvedClass"), approvedClassHome.getInstance()));
		
		return restrictions;
	}

	@Override
	protected void fetch(Root<ApprovedMatter> entity) {
		super.fetch(entity);
		entity.fetch("evidenceDefinations", JoinType.LEFT);
	}

	@Override
	protected boolean isDistinct() {
		
		return true;
	}
	
	
}
